package com.oceansoft.mobile.biz.api.controller;

import com.alibaba.fastjson.JSON;
import com.oceansoft.mobile.biz.api.entity.Member;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.common.util.Result;
import com.oceansoft.mobile.weixin.util.WeixinUtil;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lenovo on 2017/3/20.
 */
@Controller
@RequestMapping("api/legal")

public class ApiLegalController {
    /**
     * 判断是否登陆
     * @param pageName
     * @param sessiion
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "gotoIndexWithUser/{pageName}")
    public String gotoPage(@PathVariable("pageName") String pageName, HttpSession sessiion, HttpServletRequest request,
                           Model model) {
        boolean b=false;
        try{
            b = WeixinUtil.checkIsLoginUser(request);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(!b){
            model.addAttribute("userId","userId");
            model.addAttribute("openId","openId");
            String source = (String) request.getSession().getAttribute("source");
            if (!StringUtils.isEmpty(source)) {
                model.addAttribute("source", source);
            }
            return "weixin/legal/" + pageName;
        }else{
            Member member = (Member)sessiion.getAttribute(Constant.SESSION_USER);
            model.addAttribute("userId",member.getGuid());
            model.addAttribute("openId",member.getOpenId());
            model.addAttribute("userName",member.getRealName());
            model.addAttribute("mobile",member.getMobile());
            String source = (String) request.getSession().getAttribute("source");
            if (!StringUtils.isEmpty(source)) {
                model.addAttribute("source", source);
            }
            return "weixin/legal/" + pageName;
        }

    }

    /**
     * 提交数据
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "shenbao", method = RequestMethod.POST)
    public Result submitFZ(HttpServletRequest request){
        String url=Constant.host5+"api/fztsla/SaveFztsla";
       // String url="http://wsbsdt.szgaj.cn/interfacetest/api/fztsla/SaveFztsla";
        Map<String, String> param = new HashMap<>();
        param.put("OPENID",request.getParameter("openId"));
        param.put("USERGUID",request.getParameter("userguid"));
        param.put("XM",request.getParameter("xm"));
        param.put("XB",request.getParameter("xb"));
        param.put("LXDH",request.getParameter("lxdh"));
        param.put("AREA",request.getParameter("area"));
        param.put("PICGUID",request.getParameter("picture"));
        param.put("CONTENT",request.getParameter("content"));
        Result r=new Result();
        try {
            String response1= HttpClientUtil.doPost(url, param, "utf-8");
            JSON josnJson=JSON.parseObject(StringEscapeUtils.unescapeJava(response1));
            r=JSON.toJavaObject(josnJson, com.oceansoft.mobile.common.util.Result.class);
            System.out.println("法制投诉立案申报结果："+r.toString());
            return r;
        } catch (Exception e) {
            e.printStackTrace();
        }
        r.setSucc(false);
        return r;
    }

}
